package cn.edu.zisu.auth.service.impl;

import cn.edu.zisu.auth.enumeration.ColumnEnum;
import cn.edu.zisu.auth.model.LoginParam;
import cn.edu.zisu.auth.model.UserDTO;
import cn.edu.zisu.auth.repository.mysql.AccountMapper;
import cn.edu.zisu.auth.service.AuthService;
import cn.edu.zisu.common.util.Md5Util;
import cn.edu.zisu.po.AccountPo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Optional;

@Service
public class AuthServiceImpl implements AuthService {

    @Resource
    private AccountMapper accountMapper;

    @Override
    public Optional<UserDTO> loginByAccountAndPassword(LoginParam loginParam) {
        //according username to find account;
        QueryWrapper<AccountPo> wrapper = new QueryWrapper<>();
        wrapper.eq(ColumnEnum.IS_DELETE.getValue(),0);
        wrapper.eq(ColumnEnum.USERNAME.getValue(), loginParam.getUsername());
        AccountPo accountPo = accountMapper.selectOne(wrapper);

        final String password = Md5Util.md5(accountPo.getPassword());

        if(!loginParam.getPassword().equals(password)){
            return Optional.empty();
        }

        return Optional.of(new UserDTO());
    }
}
